import moment from 'moment'
import { Toast } from 'vant';
import { formatCourseList } from 'plus/unconfirmPack'
import {classPlan, submitCalendars} from 'api/getData'
import { mapGetters } from 'vuex'
export default {
    // beforeRouteEnter (to, from, next) {
    //     if(to.query.id && to.query.hcId){
    //         next()
    //     }else{
    //         Toast.fail({
    //         duration: 2000, message:'无效路径'
    //         })
    //         setTimeout(()=>{
    //             Toast.clear()
    //             next('/classPlan/classList')
    //         }, 2000)
                
    //     }
    // },
    data() {
        return {
            timeList:[],
            weekArr:['周日','周一','周二','周三','周四','周五','周六'],
            timeArr:['上午上课时间 09 : 30 - 12 : 00','下午上课时间 14 : 30 - 17 : 00','晚上上课时间 19 : 00 - 21 : 30'],
            deleteTime:false,
            dateModal:false,
            weekModal:false,
            timeModal:false,
            minDate:new Date(),  
            maxDate:new Date(),
            startDate:'',
            currentDate: '',
            weekSelected:null,
            timeSelected:null,
            courseItem:null,
            selectTimeText:'',
            currentDateModal:null,
            sOre:null,
            canSubmit:false,
            currentCourse:{},
            dataLoading: ''
        }
    },
    computed: {
        ...mapGetters(['currentUser'])
    },
    created(){
        this.dataLoading = this.getLoading('加载中')
        classPlan(this.currentUser.cluesOrStuId).
        then(res=>{
            let courseArr = []
            if(this.$route.query.type == 1) courseArr = formatCourseList(res.freeMapList)
            else courseArr = formatCourseList(res.mapList)
            let filterCourse = [];
            filterCourse = courseArr.filter(item=>{
                return (item.hcId == this.$route.query.hcId && item.id == this.$route.query.id)
            })
            this.currentCourse = filterCourse[0] 
            // console.log(filterCourse)
            if(filterCourse.length!=0){
                this.initialData(filterCourse[0].selTimeBegin,filterCourse[0].selTimeEnd)
            }else{
                //未找到对应课程空余时间
                // if(this.$route.meta.isPhone){
                //     Toast.fail({
                //     duration: 2000, message:'未找到对应课程'
                //     })
                //     setTimeout(()=>{
                //         Toast.clear()
                //         this.$router.replace('/H5classList')
                //     }, 2000)
                // }else{
                // }
                //pc端跳转
                this.$message({
                    message: '路径无效!',
                    type: 'warning',
                });
                setTimeout(()=>{
                      this.$router.replace('/classList')
                }, 1500)
            }
            this.dataLoading.close()
        }).catch(err=>{
            this.dataLoading.close()
        })
    },
    
    methods:{
        initialData(begin,end){
            this.minDate = moment(begin)._d
            this.maxDate = moment(end)._d
            this.startDate = moment(begin).format('YYYY/MM/DD')
            // console.log(this.minDate)
            // console.log(this.maxDate)
        },
        submitCourseTime(){
            // console.log(this.canSubmit)
            let sendTimeList = []
            if(this.canSubmit){
                for(var i=0;i<this.timeList.length;i++){
                    if(this.timeList[i].start!=='' && this.timeList[i].end!=='' && this.timeList[i].week!=='' && this.timeList[i].time!==''){
                        let resArr = this.getEveryWeekDay(this.timeList[i].start,this.timeList[i].end,this.timeList[i].week,this.timeList[i].time)
                        sendTimeList.push(...resArr)
                    }
                }
                // console.log(sendTimeList)
                if(sendTimeList.length == 0){
                    return
                }else{
                    submitCalendars('',sendTimeList).then(res=>{
                        // console.log(res)
                        if(res.code == '0'){
                            if(this.$route.meta.isPhone){
                                this.$router.replace('/H5submitSuccess')
                            }else{
                                this.$router.replace({
                                    path:'/webSubmitSuccess',
                                    query:{
                                        hcId:this.$route.query.hcId
                                    }
                                })
                                // this.$router.push('/classList')
                            } 
                        }
                    })
                }
                
            }
        },
        addTime(){
            let that = this;
            this.timeList.push({
                start:'',
                end:'',
                week:'',
                time:'',
                startPickerOptions:{
                    disabledDate(time) {
                        return (time.getTime() < that.minDate) || (time.getTime() > that.maxDate)
                    }
                },
                endPickerOptions:{
                    disabledDate(time) {
                        return (time.getTime() < that.minDate) || (time.getTime() > that.maxDate)
                    }
                }
            })
            // console.log(1,this.timeList)
        },
        deleteTimeItem(key){
            let copyList = this.timeList.slice()
            copyList.splice(key, 1)
            this.timeList = copyList
            this.handelCanSubmit()
        },
        showTimeSelect(bool,key){
            this.timeModal = bool
            if(bool){
                this.currentDateModal = key
                this.timeSelected = this.timeList[key].time
            }
        },
        showWeekSelect(bool,key){
            this.weekModal = bool
            if(bool){
                this.currentDateModal = key
                this.weekSelected = this.timeList[key].week
            }
        },
        selectTimeFn(key){
            this.timeSelected = key
            let copyList = this.timeList.slice()
            copyList[this.currentDateModal].time = key
            this.handelCanSubmit()
        },
        selectWeekFn(key){
            this.weekSelected = key
            let copyList = this.timeList.slice()
            copyList[this.currentDateModal].week = key
            this.handelCanSubmit()
        },
        showDeleteTime(bool){
            this.deleteTime = bool
        },
        formatter(type, value) {
            if (type === 'year') {
                return `${value}年`;
            } else if (type === 'month') {
                return `${value}月`
            }else if(type === 'day'){
                return `${value}日`
            }
            return value;
        },
        showDateModal(bool,key,sOre){
            this.dateModal = bool
            if(bool){
                this.currentDateModal = key
                this.sOre = sOre
                if(sOre == 's') this.selectTimeText = '选择开始时间'
                else this.selectTimeText = '选择结束时间'
            }
        },
        getEveryWeekDay(begin,end,week,time) {
            let resArr = []
            let days = moment(end).diff(moment(begin), 'days')
            let course = this.currentCourse.course.id
            let classPlan = this.currentCourse.id
            let stud = this.currentUser.cluesOrStuId
            for(var i=0; i<days+1; i++){
                let now = moment(begin).add(i, 'd')
                if(moment(now).day() == week){
                    let state = time === 2 ? '晚' : (time === 1? '中' : '早' )
                    resArr.push({
                        courseid: course,
                        classPlanId: classPlan,
                        state: state,
                        studate: moment(now).format('YYYY-MM-DD'),
                        stuid: stud
                    })
                }
            }
            return resArr
        },
        submitSelectTime(){
            let timeArr = this.$refs['van-datetime-picker'].$children[0].getValues()
            let copyList = this.timeList.slice()
            let time = timeArr[0] + '/' + timeArr[1] + '/' + timeArr[2];
            let start = copyList[this.currentDateModal].start
            let end = copyList[this.currentDateModal].end
            if(this.sOre == 's') {
                if(end && moment(end).format('X') < moment(time).format('X')){
                    Toast('开始时间不能晚于结束时间')
                    return
                }else{
                    copyList[this.currentDateModal].start = time
                }
                

            }
            else {  
                if(start && moment(start).format('X') > moment(time).format('X')){
                    Toast('结束时间不能早于开始时间')
                    return
                }else{
                    copyList[this.currentDateModal].end = time
                }
                
            }
            this.timeList = copyList
            this.showDateModal(false,'','')
            this.handelCanSubmit()
        },
        handelCanSubmit(){
            if(this.timeList.length === 0){
                this.canSubmit = false
                return
            }
            for(var i=0;i<this.timeList.length;i++){
                // console.log(this.timeList[i])
                if(this.timeList[i].start!=='' && this.timeList[i].end!=='' && this.timeList[i].week!=='' && this.timeList[i].time!==''){
                    this.canSubmit = true;
                    break;
                }else{
                    this.canSubmit = false;
                }
            }
            // console.log(this.canSubmit)
        }
    }
}